package com.app114.app.android.rk;

import java.io.ByteArrayOutputStream;
import java.util.HashMap;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;

import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.umeng.api.sns.UMSnsService;

public class ShareDialog extends BaseFragmentActivity {
	String shotPath;
	String title;
	// private OAuthV2 oAuth;
	private Bitmap mBitmap;

	@Override
	protected void activitySetup(Bundle savedInstanceState) {
		getSupportActionBar().setTitle("分享");
		setContentView(R.layout.dialog_share);
		ImageView shot = (ImageView) findViewById(R.id.shot);
		shotPath = getIntent().getStringExtra("data");
		mBitmap = BitmapFactory.decodeFile(shotPath);
		title = getIntent().getStringExtra("title");
		shot.setImageBitmap(mBitmap);
	}

	@Override
	protected boolean _onCreateOptionsMenu(Menu menu) {
		getSupportMenuInflater().inflate(R.menu.activity_share, menu);
		return true;
	}

	@Override
	protected boolean _onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_share:
			share();
			// ShareActionProvider actionProvider = (ShareActionProvider) item.getActionProvider();
			// actionProvider.setShareHistoryFileName(ShareActionProvider.DEFAULT_SHARE_HISTORY_FILE_NAME);
			// // Note that you can set/change the intent any time,
			// // say when the user has selected an image.
			// actionProvider.setShareIntent(createShareIntent());
			// try {
			// oAuth = SharedPrefsUtil.getObject(this, Conf.KEY_OAUTH_T_QQ, null);
			// String tips = "";
			// if (oAuth != null) {
			// String result = QWeiboHandle.SendMessageWithPic(oAuth, title, shotPath);
			// JSONObject jo = new JSONObject(result);
			// String msg = jo.optString("msg");
			// if (msg.equals("ok")) {
			// tips = "分享成功";
			// UmengUtil.onEvent(mContext, Conf.EVENT_SNS_SHARE_QQ, result);
			// finish();
			// } else {
			// tips = msg;
			// }
			// } else {
			// tips = "请先绑定微博帐号";
			// }
			// Toast.makeText(this, tips, 1).show();
			// } catch (Exception e) {
			// Logger.e(TAG, e.getLocalizedMessage());
			// }
			break;

		default:
			break;
		}
		return true;
	}

	private void share() {

		ByteArrayOutputStream stream = new ByteArrayOutputStream();
		mBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
		final byte[] picture = stream.toByteArray();

		if (mBitmap != null && !mBitmap.isRecycled()) {
			mBitmap.recycle();
		}

		HashMap<String, String> map = new HashMap<String, String>();
		UMSnsService.shareToTenc(this, picture, map, null);
	}

	/**
	 * Creates a sharing {@link Intent}.
	 * 
	 * @return The sharing intent.
	 */
	private Intent createShareIntent() {
		Intent shareIntent = new Intent(Intent.ACTION_SEND);
		shareIntent.setType("text/html");
		shareIntent.putExtra(Intent.EXTRA_STREAM, "");
		return shareIntent;
	}

	@Override
	protected void activityDestory() {
		// TODO Auto-generated method stub

	}
}
